//34. 在排序数组中查找元素的第一个和最后一个位置

#include <vector>
using namespace std;

class Solution 
{
public:
    vector<int> searchRange(vector<int>& nums, int target)
    {
        // 区间合理性判断
        if(0 == nums.size()) return {-1, -1};

        int begin = 0;
        int left = 0, right = nums.size() - 1;
        // 找左端点
        while(left < right)
        {
            int mid = left + (right - left) / 2; // 防溢出
            if(nums[left] < target) left = mid + 1;
            else right = mid;
        }
        if(nums[left] != target) return {-1, -1};
        else begin = left;

        // 找右端点
        left = 0, right = nums.size() - 1;
        while(left < right)
        {
            int mid = left + (right - left + 1) / 2; // 防溢出
            if(nums[mid] <= target) left = mid;
            else right = mid - 1;
        }
        return {begin, right};
    }
};